﻿/* $RCSFile: DocumentsHelper.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/DocumentsHelper.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using AddonNice.Diagnostics;
using System.Diagnostics;
using System.IO;
using AddonNice.Settings;
using System.Web;
using System.Globalization;

namespace AddonNice.Helpers
{
    public class DocumentsHelper
    {
        static Hashtable _availExtensions          =   null;
        public static Hashtable AvailExtensions
        {
            get
            {
                if ( _availExtensions != null )
                    return _availExtensions;
                string key          =   "Document_AvailExtensions";
                if ( AddonNice.Settings.Cache.CurrentCache.Exists(key) )
                {
                    _availExtensions =   (Hashtable)AddonNice.Settings.Cache.CurrentCache.Get(key);
                    return _availExtensions;
                }
                if (CoreTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( "[V]DocumentsHelper AvailExtensions GET BEG.",CoreTraceSwitch.Sw.Info);
                string baseImageDIR =   Config.PortalDocumentExtensionPath;
                string bDir         =   HttpContext.Current.Server.MapPath(baseImageDIR);
                DirectoryInfo di    =   new DirectoryInfo(bDir);
                FileInfo[] rgFiles  =   di.GetFiles("*.gif");
                string ext          =   string.Empty;
                string nme          =   string.Empty;
                string f_Name       =   string.Empty;
                _availExtensions    =   new Hashtable();
                foreach (FileInfo fi in rgFiles)
                {
                    f_Name          =   fi.Name;
                    ext             =   fi.Extension;
                    nme             =   f_Name.Substring(0, (f_Name.Length - ext.Length));
                    _availExtensions.Add(nme, f_Name);
                }
                if ( _availExtensions.Count > 0 )
                    AddonNice.Settings.Cache.CurrentCache.InsertVLong(key,_availExtensions);
                if (CoreTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( "[V]DocumentsHelper LoadAvailableImageList end.",CoreTraceSwitch.Sw.Info);
                return _availExtensions;
            }
        }

        /// <summary>
        /// Assign image corresponding to the default extensions or doc type
        /// </summary>
        public static string GetImage(string contentType)
        {
            if (CoreTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DocumentsHelper GetImage BEG contentType: {0}.",contentType),CoreTraceSwitch.Sw.Info);
            if ( DocumentsHelper.AvailExtensions.ContainsKey(contentType) )
                return DocumentsHelper.AvailExtensions[contentType].ToString();
            return "unknown.gif";
        }

    }
}
